package com.lucifer.treasure;

import com.lucifer.treasure.util.SwordAuraRenderer;
import com.lucifer.treasure.enchant.effect.SwordAuraEnchantmentEffect;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.World;

import java.util.UUID;

/**
 * 🎯 御剑术客户端渲染器 - 在客户端环境中处理逻辑
 */
public class SwordAuraClientRenderer {
    
    private static long lastDebugTime = 0;
    private static final long DEBUG_INTERVAL = 5000; // 每5秒输出一次调试信息
    
    /**
     * 客户端渲染方法 - 在客户端环境中调用服务端逻辑
     */
    public static void renderClientSwordAura(PlayerEntity player, World world, int auraLevel) {
        if (player == null || world == null || auraLevel <= 0) {
            return;
        }
        
        UUID playerId = player.getUuid();
        
        // 🎯 在客户端环境中，尝试获取服务端世界
        MinecraftClient client = MinecraftClient.getInstance();
        if (client.getServer() != null && !world.isClient) {
            // 单机模式：直接调用服务端逻辑
            SwordAuraRenderer.renderSwordAura(player, world, auraLevel);
        } else if (client.getServer() != null && world.isClient) {
            // 单机模式的客户端：获取服务端世界
            net.minecraft.server.MinecraftServer server = client.getServer();
            ServerWorld serverWorld = server.getWorld(world.getRegistryKey());
            if (serverWorld != null) {
                // 获取服务端玩家
                net.minecraft.server.network.ServerPlayerEntity serverPlayer = server.getPlayerManager().getPlayer(playerId);
                if (serverPlayer != null) {
                    // 🎯 客户端调用服务端逻辑
                    SwordAuraEnchantmentEffect.maintainFloatingSwordsSmartly(serverWorld, serverPlayer, auraLevel, world.getTime());
                }
            }
        } else {
            // 多人游戏模式：这里应该通过网络包通信，但现在先使用简化处理
            // 只调用统一渲染器的客户端逻辑
            SwordAuraRenderer.renderSwordAura(player, world, auraLevel);
        }
    }
    
    // ==================== 数据访问方法 ====================
    
    public static int getClientVisibleSwordCount(UUID playerId) {
        MinecraftClient client = MinecraftClient.getInstance();
        
        if (client != null && client.getServer() != null) {
            // 🎯 单机模式：客户端可以直接访问服务端数据
            return SwordAuraEnchantmentEffect.getVisibleFloatingSwordCount(playerId);
        } else {
            // 🎯 多人游戏模式：使用客户端玩家的御剑术等级估算
            if (client != null && client.player != null) {
                int auraLevel = SwordAuraEnchantmentEffect.getPlayerSwordAuraLevel(client.player);
                boolean hasAura = auraLevel > 0;
                int estimatedCount = hasAura ? auraLevel : 0;
                
                // 🧪 定期输出调试信息（降低频率）
                long currentTime = System.currentTimeMillis();
                if (currentTime - lastDebugTime > DEBUG_INTERVAL) {
                    System.out.println("🧪 [多人游戏] 御剑术等级: " + auraLevel + ", 估算剑数: " + estimatedCount);
                    lastDebugTime = currentTime;
                }
                
                return estimatedCount;
            }
            return 0;
        }
    }
    
    public static int getClientAuraLevel(UUID playerId) {
        MinecraftClient client = MinecraftClient.getInstance();
        if (client.getServer() != null && client.player != null) {
            // 单机模式：获取服务端玩家数据
            net.minecraft.server.network.ServerPlayerEntity serverPlayer = 
                client.getServer().getPlayerManager().getPlayer(playerId);
            if (serverPlayer != null) {
                return SwordAuraEnchantmentEffect.getPlayerSwordAuraLevel(serverPlayer);
            }
        }
        // 多人游戏模式：获取客户端玩家数据
        if (client != null && client.player != null && client.player.getUuid().equals(playerId)) {
            return SwordAuraEnchantmentEffect.getPlayerSwordAuraLevel(client.player);
        }
        return 0;
    }
    
    public static boolean getClientAuraEnabled(UUID playerId) {
        MinecraftClient client = MinecraftClient.getInstance();
        
        if (client != null && client.getServer() != null) {
            // 🎯 单机模式：直接访问服务端状态
            return SwordAuraEnchantmentEffect.isAuraEnabled(playerId);
        } else {
            // 🎯 多人游戏模式：这里应该通过网络包同步状态
            // 暂时使用简化逻辑：如果有御剑术附魔就认为是开启的
            if (client != null && client.player != null && client.player.getUuid().equals(playerId)) {
                int auraLevel = SwordAuraEnchantmentEffect.getPlayerSwordAuraLevel(client.player);
                boolean hasAura = auraLevel > 0;
                
                // 🧪 定期输出调试信息
                long currentTime = System.currentTimeMillis();
                if (currentTime - lastDebugTime > DEBUG_INTERVAL) {
                    System.out.println("🧪 [多人游戏] 御剑术状态检查 - 等级: " + auraLevel + ", 状态: " + (hasAura ? "开启" : "关闭"));
                }
                
                return hasAura;
            }
            return false;
        }
    }
    
    public static int getClientMaxSwordCount(UUID playerId) {
        return getClientAuraLevel(playerId);
    }
    
    public static int getClientHitEffectSwordCount(UUID playerId) {
        MinecraftClient client = MinecraftClient.getInstance();
        if (client.getServer() != null) {
            // 单机模式：访问服务端数据
            return SwordAuraEnchantmentEffect.getCurrentHitEffectSwordCount(playerId);
        }
        // 多人游戏模式：无法获取精确数据
        return 0;
    }
    
    public static int getClientRecoveringSwordCount(UUID playerId) {
        MinecraftClient client = MinecraftClient.getInstance();
        if (client.getServer() != null) {
            // 单机模式：访问服务端数据
            return SwordAuraEnchantmentEffect.getCurrentRecoveringSwordCount(playerId);
        }
        // 多人游戏模式：无法获取精确数据
        return 0;
    }
    
    public static java.util.List<com.lucifer.treasure.enchant.effect.SwordAuraPvPSystem.HostilityInfo> getClientHostilities(UUID playerId) {
        MinecraftClient client = MinecraftClient.getInstance();
        if (client.getServer() != null) {
            // 单机模式：访问服务端数据
            net.minecraft.server.network.ServerPlayerEntity serverPlayer = 
                client.getServer().getPlayerManager().getPlayer(playerId);
            if (serverPlayer != null) {
                return SwordAuraEnchantmentEffect.getPlayerHostilities(serverPlayer);
            }
        }
        // 多人游戏模式：无法获取精确数据
        return new java.util.ArrayList<>();
    }
    
    /**
     * 清理客户端数据
     */
    public static void clearClientData(UUID playerId) {
        SwordAuraRenderer.clearPlayerData(playerId);
    }
} 